import { APPEND_LIST, PREPEND_LIST } from './../mutation-type'
import { getGalleryList, getWhatsHot, getListByApi } from './../../service/getData'
import { common } from './../modules/common'
import { generateSLang, formatDateTime } from './../../config/mUtils'

function getQuery () {
  let searchQuery = null
  if (common.state.search.advanceSearchEnabled) {
    searchQuery = Object.assign({}, common.state.search.searchParams.basicSearch, common.state.search.searchParams.advSearch)
  } else {
    searchQuery = Object.assign({}, common.state.search.searchParams.basicSearch)
  }
  return searchQuery
}

async function initHomeList ({commit, state}, page) {
  let searchParams = Object.assign({}, {page: page}, getQuery())
  state.loadMoreDisabled = true
  state.data = []
  state.showLoading = true
  try {
    let listData = await getGalleryList(searchParams)
    state.showLoading = false
    state.loadMoreDisabled = false
    state.page = Number(listData.payload.page)
    state.pageNumber = listData.payload.pageNumber
    state.startPage = page
    state.data = listData.payload.payload
  } catch (e) {
    console.log(e)
  }
}

export default {
  initHomeList,
  async initWhatsHot ({commit, state}) {
    state.showLoading = true
    state.data = []
    try {
      let whatsHot = await getWhatsHot()
      let whatsHotData = await getListByApi(whatsHot.payload, false)
      whatsHotData.gmetadata.forEach(function (item, i) {
        item.date = formatDateTime(Number(item.posted) * 1000)
        item.lang = generateSLang(item.title)
      })
      state.showLoading = false
      state.page = 1
      state.pageNumber = 1
      state.data = whatsHotData.gmetadata
    } catch (e) {
      console.log(e)
    }
  },
  async toogleEX ({commit, state}) {
    commit('SWITCH_SOURCE')
    initHomeList({commit, state}, 0)
  },
  async toogleSafeMode ({commit, state}) {
    commit('SWITCH_SAFEMODE')
    initHomeList({commit, state}, 0)
  },
  async homeListLoadMore ({commit, state}) {
    if (state.loadMore === true || state.loadMoreDisabled === true) {
      return
    }
    state.loadMore = true
    try {
      let searchParams = Object.assign({}, {page: state.page + 1}, getQuery())
      let listData = await getGalleryList(searchParams)
      state.loadMore = false
      state.page += 1
      commit(APPEND_LIST, {params: 'homeList', data: listData.payload.payload})
      if (listData.payload.payload < 20) {
        state.loadMoreDisabled = true
        return
      }
      state.loadMore = false
    } catch (e) {
      console.log(e)
    }
  },
  async homeListLoadTop ({commit, state}, { refs }) {
    let page = null
    if (state.startPage === 0) {
      page = 0
    } else {
      page = state.startPage - 1
    }
    try {
      let searchParams = Object.assign({}, {page: page}, getQuery())
      let listData = await getGalleryList(searchParams)
      commit(PREPEND_LIST, {params: 'homeList', data: listData.payload.payload})
      refs.loadmore.onTopLoaded()
    } catch (e) {
      console.log(e)
    }
  }
}
